import { postLoginAPI } from '@/api/user'
import router from '@/router'
// import router from '@/router'
import { getToken, setToken } from '@/utils/auth'
import { Message } from 'element-ui'

const state = {
  // vuex存token
  token: getToken()
}

const mutations = {
  // 存token
  saveToken(state, payload) {
    state.token = payload
  }
}

const actions = {
  async loginAction({ commit }, payload) {
    const res = await postLoginAPI(payload)
    console.log('res  ----->  ', res)
    // 本地存储保存一份token
    setToken(res.data.token.token)
    // vuex存一份token
    commit('saveToken', res.data.token.token)
    // 跳转页面
    router.push('/')
    // 提示用户登录成功
    Message.success('登录成功')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

